package activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;

import net.ck.hzq.R;
import net.ck.hzq.TobuyApplication;


import org.greenrobot.eventbus.EventBus;

import Utils.ToastUtil;
import base.BaseActivity;
import base.BaseBiz;
import dao.DirectionalDevicesInput;
import dialog.DirectionalAllocationDialog;
import dialog.DirectionalAllocationFalseDialog;
import dialog.DirectionalAllocationLiuZhuanDialog;
import dialog.DirectionalAllocationResDialog;
import event.DirectionalResEvent;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import ui.activity.home.PickupMachineAct;
import ui.activity.login.biz.LoginBiz;
import ui.activity.profit.biz.ProfitBiz;
import ui.model.IntegralTotalBean;

public class MyJiFenActivity extends BaseActivity {


    TextView zhanghujifen;
    TextView zuorijifen;
    TextView huodejifen;
    TextView shiyongjifen;
    @Override
    public int getLayoutId() {
        return R.layout.activity_myjifen;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        findViewById(R.id.iv_back).setOnClickListener(v -> finish());
        zhanghujifen = findViewById(R.id.zhanghujifen);
        zuorijifen = findViewById(R.id.zuorijifen);
        huodejifen = findViewById(R.id.huodejifen);
        shiyongjifen = findViewById(R.id.shiyongjifen);


        findViewById(R.id.share_but_invitation).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MyJiFenActivity.this, PickupMachineAct.class);
                startActivity(intent);
            }
        });
        findViewById(R.id.lineargetjifen).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MyJiFenActivity.this, GetJIFenRecordAct.class);
                startActivity(intent);
            }
        });
        findViewById(R.id.usejifen).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MyJiFenActivity.this, UseJIFenRecordAct.class);
                startActivity(intent);
            }
        });
        findViewById(R.id.liuzhuanjilu).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MyJiFenActivity.this, LiuZhuanRecordAct.class);
                startActivity(intent);
            }
        });

        findViewById(R.id.jieshoujilu).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MyJiFenActivity.this, jieshouRecordAct.class);
                startActivity(intent);
            }
        });
        findViewById(R.id.liuzhuan).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showdialog();
            }
        });

        getData();
    }

    private void getData(){
        new LoginBiz().integralTotal(TobuyApplication.customId, new BaseBiz.Callback<IntegralTotalBean>() {
            @Override
            public void onSuccess(IntegralTotalBean integralTotalBean) {
                huodejifen.setText(integralTotalBean.getInTotal());
                zuorijifen.setText(integralTotalBean.getCurDayIntegral());
                zhanghujifen.setText(integralTotalBean.getIntegral());
                shiyongjifen.setText(integralTotalBean.getOutTotal());

            }

            @Override
            public void onFailure(IntegralTotalBean integralTotalBean) {

            }
        });


    }

    private void showdialog() {
        DirectionalAllocationLiuZhuanDialog dialog = DirectionalAllocationLiuZhuanDialog.newInstance();
        dialog.setConfirmListener((String name, String tel,String num) -> {


            DirectionalDevicesInput a = new DirectionalDevicesInput();
            a.jifen = num;
            a.fromId = TobuyApplication.getCustomId();
            a.toName = name;
            a.toPhone = tel;
            RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), new Gson().toJson(a));
           new ProfitBiz().flowliuzhuan(requestBody, new BaseBiz.Callback<Boolean>() {
                @Override
                public void onSuccess(Boolean info) {
                    dialog.dismiss();
                    ToastUtil.show(MyJiFenActivity.this,"流转成功！");
                    getData();
                }

                @Override
                public void onFailure(Boolean info) {
                    dialog.dismiss();
                    ToastUtil.show(MyJiFenActivity.this,"流转失败！");

                }
            });
        });
        dialog.show(getSupportFragmentManager(), "");
    }

}
